package app.android.alampang.ui.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Adapter;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import java.io.IOException;
import java.util.ArrayList;

import app.android.alampang.R;

/**
 * Created by Lihong on 13/02/2015.
 */
public class QuiltView extends FrameLayout implements OnGlobalLayoutListener {

    public QuiltViewBase quilt;
    public ViewGroup scroll;
    public int padding = 5;
    public boolean isVertical = false;
    public ArrayList<View> views;
    private Adapter adapter;


    public QuiltView(Context context, boolean isVertical) {
        super(context);
        this.isVertical = isVertical;
        setup();
    }

    public QuiltView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.QuiltView);

        String orientation = a.getString(R.styleable.QuiltView_scrollOrientation);
        if (orientation != null) {
            if (orientation.equals("vertical")) {
                isVertical = true;
            } else {
                isVertical = false;
            }
        }
        setup();
    }

    public void setup() {
        views = new ArrayList<View>();

        if (isVertical) {
            scroll = new ScrollView(this.getContext());
        } else {
            scroll = new HorizontalScrollView(this.getContext());
        }
        quilt = new QuiltViewBase(getContext(), isVertical);
        scroll.addView(quilt);
        this.addView(scroll);

    }

    private DataSetObserver adapterObserver = new DataSetObserver() {
        public void onChanged() {
            super.onChanged();
            onDataChanged();
        }

        public void onInvalidated() {
            super.onInvalidated();
            onDataChanged();
        }

        public void onDataChanged() {
            setViewsFromAdapter(adapter);
        }
    };

    public void setAdapter(Adapter adapter) {
        this.adapter = adapter;
        adapter.registerDataSetObserver(adapterObserver);
        setViewsFromAdapter(adapter);
    }

    private void setViewsFromAdapter(Adapter adapter) {
        this.removeAllViews();
        for (int i = 0; i < adapter.getCount(); i++) {
            quilt.addPatch(adapter.getView(i, null, quilt));
        }
    }

    public void addPatchImages(ArrayList<ImageView> images) {

        for (ImageView image : images) {
            addPatchImage(image);
        }
    }

    public void addPatchImage(ImageView image) {

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        image.setLayoutParams(params);

        LinearLayout wrapper = new LinearLayout(this.getContext());
        wrapper.setPadding(padding, padding, padding, padding);
        wrapper.addView(image);
        quilt.addPatch(wrapper);
    }

    public void addPatchImageUri(String uri) {
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inScaled = true;
        bmOptions.inSampleSize = 2;
        Bitmap bitmap = BitmapFactory.decodeFile(uri, bmOptions);

        // check orientation
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(uri);
        } catch (IOException e) {
        }
        int orientation = 6;
        if (exif != null) {
            orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        }

        Matrix matrix = new Matrix();
        if (orientation == 6) {
            matrix.postRotate(90);
        } else if (orientation == 3) {
            matrix.postRotate(180);
        } else if (orientation == 8) {
            matrix.postRotate(270);
        }

        Bitmap pBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

        ImageView img = new ImageView(getContext());
        img.setImageBitmap(pBitmap);
        img.setScaleType(ImageView.ScaleType.CENTER_CROP);

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        img.setLayoutParams(params);

        LinearLayout wrapper = new LinearLayout(this.getContext());
        wrapper.setPadding(padding, padding, padding, padding);
        wrapper.addView(img);
        quilt.addPatch(wrapper);
    }

    public void addPatchViews(ArrayList<View> views_a) {
        for (View view : views_a) {
            quilt.addPatch(view);
        }
    }

    public void addPatchView(View view) {
        quilt.addPatch(view);
    }

    public void addPatchesOnLayout() {
        for (View view : views) {
            quilt.addPatch(view);
        }
    }

    public void removeQuilt(View view) {
        quilt.removeView(view);
    }

    public void setChildPadding(int padding) {
        this.padding = padding;
    }

    public void refresh() {
        quilt.refresh();
    }

    public void setOrientation(boolean isVertical) {
        this.isVertical = isVertical;
    }


    @Override
    public void onGlobalLayout() {
        //addPatchesOnLayout();
    }
}